package gupao.concurrency.demo.risk.model;

import java.util.ArrayList;
import java.util.List;

public class NodeTreeMock {

    public static NodeTree mockRiskJobTree(){
        List<Node> list = generateNodes();
        return NodeTree.buildTree(list);
    }

    private static List<Node> generateNodes(){
        //level 0: 1000
        List<Node> level0 = new ArrayList<>(1000);
        for(long i = 1; i <= 1000; i++){
            level0.add(createNode(i,0));
        }
        List<Node> level1 = new ArrayList<>(100);
        for(long i = 1001; i <= 1100; i++){
            level1.add(createNode(i,1));
        }
        List<Node> level2 = new ArrayList<>(10);
        for(long i = 1101; i <= 1110; i++){
            level2.add(createNode(i,2));
        }
        Node root = createNode(1111L, 3);
        List<Node> nodes = new ArrayList<>();
        nodes.addAll(level0);
        nodes.addAll(level1);
        nodes.addAll(level2);
        nodes.add(root);

        //set parent
        level2.forEach(c -> c.setParentId(root.getNodeId()));
        setRelationShip(level2, level1);
        setRelationShip(level1, level0);

        return nodes;
    }

    private static void setRelationShip(List<Node> parents, List<Node> children){
        int indexP = 0, count = 0;
        for (Node c : children) {
            c.setParentId(parents.get(indexP).getNodeId());
            if(++count == 10){
                indexP++;
                count = 0;
            }
        }
    }

    private static Node createNode(Long id, int level){
        Node n = new Node();
        n.setNodeId(id);
        n.setLevel(level);
        return n;
    }
}
